module.exports = class OnEmitRemoveMan {
    constructor(){
        this.planObj = {};
    }
    on(cmd, fn){
        if(this.planObj[cmd] == undefined) this.planObj[cmd] = [];
        this.planObj[cmd].push(fn);
    }
    emit(cmd){
        const arg = [...arguments].slice(1);
        this.planObj[cmd].forEach((item, index) => {
            item(...arg);
            // console.log(this)
        })
    }
    remove(cmd, fn){
        if(this.planObj[cmd] == undefined) throw `${cmd} event has not bind to this.planObj`;
        if(this.planObj[cmd].every((item)=>item!==fn)) console.log(`${cmd} event stack has no special function.`);
        this.planObj[cmd] = this.planObj[cmd].filter(function (item, index) {
            return item !== fn;
        })
    }
    removeFirst(cmd, fn){
        if(this.planObj[cmd] == undefined) throw `${cmd} event has not bind to this.planObj`;
        if(this.planObj[cmd].every((item)=>item!==fn)) console.log(`${cmd} event stack has no special function.`);
        const rmIndex = this.planObj[cmd].findIndex(function (item) {
            return item === fn;
        });
        this.planObj[cmd].splice(rmIndex, 1);
    }
}
